<?php

namespace app\common\model;

use think\Db;
use think\Model;

/**
 * 以太坊代币转账模型
 * 需要先部署节点，有地址才能用
 * https://blog.csdn.net/Little_Ji/article/details/103138502
 * ---未测试---
 */
class EthTrans extends Model
{
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $usdt_contract_address = '0xdac17f958d2ee523a2206206994597c13d831ec7';


    protected static function init()
    {
    }

    /**
     * @param $url  节点url 地址   "http://localhost:8845"
     * @param $post_data  发起请求post数据  $opts
     * @return array
     */
    public function curlPost($url, $post_data)
    {
        $curl = curl_init();
        $jsonStringData = json_encode( $post_data );
        $options = [
            CURLOPT_URL => $url,
            CURLOPT_POST => true,
            CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0",
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_TIMEOUT => 20,
            CURLOPT_HTTPHEADER => [
                'Content-Type: application/json',
                'Content-Length: ' . strlen( $jsonStringData )
            ],
        ];
        curl_setopt_array( $curl, $options );
        //设置post数据
        curl_setopt( $curl, CURLOPT_POSTFIELDS, $jsonStringData );
        //执行命令
        $data = curl_exec( $curl );
        //关闭URL请求
        if ( $data ) {
            curl_close( $curl );

            $res_data = json_decode( $data, true );

            return ['status' => true, 'data' => $res_data];
        } else {
            $error = curl_errno( $curl );
            curl_close( $curl );
            return ['status' => false, 'data' => '', 'msg' => $error];
        }
    }

    /**
     * 解锁账户
    */
    public function unlock_account()
    {
        $opts = [
            'jsonrpc' => '2.0',
            'method' => 'personal_unlockAccount',	//解锁账户接口
            'params' => [
                '0xb60e8dd61c5d32be8058bb8eb970870f07233155',	//解锁账户地址
                '123456'	//账户密码
            ],
            'id' => time()
        ];

    }

    /**
     * 锁定账户
    */
    public function lock_account()
    {
        $opts = [
            'jsonrpc' => '2.0',
            'method' => 'personal_lockAccount',	//锁定账户接口
            'params' => [
                '0xb60e8dd61c5d32be8058bb8eb970870f07233155'	//锁定账户地址
            ],
            'id' => time()
        ];
    }

    /**
     * 查询账户余额
    */
    public function get_balance()
    {
        $opts = [
            'jsonrpc' => '2.0',
            'method' => 'eth_call', //查询以太坊代币的jsonrpc接口
            'params' => [
                0=>[
                    'from' =>'0xb60e8dd61c5d32be8058bb8eb970870f07233155',//查询地址
                    'to' => '0xdac17f958d2ee523a2206206994597c13d831ec7',    //usdt 智能合约地址
                    'data' => '0x70a08231000000000000000000000000' . substr( '0xb60e8dd61c5d32be8058bb8eb970870f07233155', 2),
                    //0x70a08231000000000000000000000000 为固定用法, 后面加上去掉0x的from
                ],
                1=>'latest'	//”latest”, “earliest” or “pending”

            ],
            'id' => time()
        ];
    }

    /**
     * 发起交易
    */
    public function translate()
    {
        $opts = [
            'jsonrpc' => '2.0',
            'method' => 'eth_sendTransaction', //转账接口
            'params' => [
                0=>[
                    'from' =>'0xb60e8dd61c5d32be8058bb8eb970870f07233155',//转账地址
                    'to' => '0xdac17f958d2ee523a2206206994597c13d831ec7',    //usdt 智能合约地址
                    'value'=>'0x', //合约交易, value为0
                    'gas'=>'0x' . dechex( '1000000' ),  // 1000000 可根据自己修改, 少的话,交易失败率高
                    'gasPrice'=>'0x' . dechex( '1000000000' ), //同 gas
                    'data' => '0xa9059cbb000000000000000000000000b60e8dd61c5d32be8058bb8eb970870f0723315500000000000000000000000000000000000000000000000000000000000f4240'
                    //data参数: '0xa9059cbb'.处理的接收地址 . 处理的交易金额
                    //处理的接收地址: 地址去掉0x , 然后在前补0到64位
                    //处理的交易金额: 交易金额转成16进制,如果带0x 去掉0x,不带0x忽略, 然后在前方补0到64位
                ],

            ],
            'id' => time()
        ];
    }
}
